home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11600 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.5 KB  |  253 lines

  1. Path: nntpserver.sp.ac.sg.!usenet
  2. From: ABC <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Help on display names
  5. Date: 25 Mar 1996 08:49:45 GMT
  6. Organization: Singapore Polytechnic
  7. Message-ID: <4j5mn9$mn2@scctn01.sp.ac.sg>
  8. NNTP-Posting-Host: 164.78.196.219
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------22081592413785"
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. ---------------------------------22081592413785
  17. Content-Transfer-Encoding: 7bit
  18. Content-Type: text/plain; charset=us-ascii
  19.  
  20. I need to display a list of names before and after my ADD function
  21. I managed to display a list of names, compute my function, but when i 
  22. display the names again, my display function will cock up (display NULL).
  23. Can anyone please give some guidance on this subject?
  24. Thanks
  25.  
  26.  
  27. ---------------------------------22081592413785
  28. Content-Transfer-Encoding: 7bit
  29. Content-Type: text/plain
  30.  
  31. #include<stdio.h>
  32. #include<alloc.h>
  33. #include<conio.h>
  34. #include<string.h>
  35. #include<ctype.h>
  36. #include<stdlib.h>
  37. #define NSIZE 30     //--array of size to hold name
  38. #define ASIZE 100    //--array of size to hold address
  39. #define FILENAME "B:DATA.DAT"
  40.  
  41. typedef struct data
  42. {
  43.     char name[NSIZE];
  44.     char address[ASIZE];
  45.     int age;
  46.     struct data *next;
  47. }NodeItem;
  48. typedef NodeItem *Nodeptr;
  49.  
  50. void menu(void);
  51. void load_record(FILE *fp, Nodeptr *headptr, Nodeptr *lastptr);
  52. void create_link_list(Nodeptr *headptr, Nodeptr *lastptr);
  53. void display_record(Nodeptr *headptr);
  54. void save_record(FILE *fp, Nodeptr *headptr);
  55.  
  56. int main()
  57. {
  58.      Nodeptr headptr,lastptr;
  59.      char ans1;
  60.      char choice;
  61.  
  62.      FILE *fp;
  63.      headptr=NULL;
  64.      load_record(fp, &headptr, &lastptr);
  65.      do
  66.      {
  67.       menu();
  68.       choice=getchar();
  69.       choice=toupper(choice);
  70.       flushall();
  71.       switch(choice)
  72.       {
  73.         case 'A': flushall();
  74.               clrscr();
  75.               display_record(&headptr);
  76.               create_link_list(&headptr, &lastptr);
  77.               break;
  78.         case 'B': flushall();
  79.               save_record(fp, &headptr);
  80.               break;
  81.         case 'C': exit(1);
  82.         default : break;
  83.       }
  84.       }while(choice!='C');
  85.       return 0;
  86. }
  87.  
  88. void menu(void)
  89. {
  90.      printf("\n\nA) ENTER PARTICULAR(S)\n");
  91.      printf("B) SAVE\n");
  92.      printf("C) EXIT\n");
  93.      printf("\nEnter your choice : ");
  94.      return;
  95. }
  96.  
  97. void create_link_list(Nodeptr *headptr, Nodeptr *lastptr)
  98. {
  99.      int node_size=sizeof(NodeItem);
  100.      Nodeptr ptr;
  101.      char answer='N';
  102.  
  103.      do
  104.      {
  105.       ptr=(Nodeptr)malloc(node_size);
  106.       printf("\n\nÜÜÜÜÜÜÜÜÜÜ  ENTER PARTICULAR(S)  ÜÜÜÜÜÜÜÜÜÜ\n");
  107.       printf("\nPlease enter name   : ");
  108.       gets(ptr->name);
  109.       if(ptr->name[0]=='0')
  110.       {
  111.            return;
  112.       }
  113.       while(ptr->name[0]=='\0')
  114.       {
  115.            printf("\nPlease enter name   : ");
  116.            gets(ptr->name);
  117.       }
  118.       strcpy(ptr->name, strupr(ptr->name));
  119.       flushall();
  120.       printf("\nPlease enter address : ");
  121.       gets(ptr->address);
  122.       while(ptr->address[0]=='\0')
  123.       {
  124.            printf("\nPlease enter address:");
  125.            gets(ptr->address);
  126.       }
  127.       strcpy(ptr->address,strupr(ptr->address));
  128.       flushall();
  129.       printf("\nPlease enter age :    years old\b\b\b\b\b\b\b\b\b\b\b\b");
  130.       scanf("%d", &(ptr->age));
  131.       while(ptr->age< 1 || ptr->age>100)   //--check for invalid age
  132.       {
  133.            printf("\nPlease enter age :     years old\b\b\b\b\b\b\b\b\b\b\b\b");
  134.            scanf("%d", &(ptr->age));
  135.       }
  136.       flushall();
  137.       if((*headptr)==NULL)
  138.       {
  139.            (*headptr)=ptr;
  140.            (*lastptr)=ptr;
  141.       }
  142.       else
  143.       {
  144.            (*lastptr)->next=ptr;
  145.            (*lastptr)=ptr;
  146.       }
  147.       printf("\nAny more? <Y/N> : ");
  148.       answer=getchar();
  149.       answer=toupper(answer);
  150.       flushall();
  151.      }while(answer!='N');
  152.      (*lastptr)->next=NULL;
  153.      return;
  154. }
  155.  
  156. void display_record(Nodeptr *headptr)
  157. {
  158.      Nodeptr ptr;
  159.      int y;
  160.  
  161.      ptr=(*headptr);
  162.      while(ptr!=NULL)
  163.      {
  164.      for(y=1;y<10;y++)
  165.      {
  166.          gotoxy(1,y);printf("%s", ptr->name);
  167.          ptr=ptr->next;
  168.      }
  169.      for(y=1;y<10;y++)
  170.      {
  171.          gotoxy(25,y);printf("%s", ptr->name);
  172.          ptr=ptr->next;
  173.      }
  174.      for(y=1;y<10;y++)
  175.      {
  176.          gotoxy(50,y);printf("%s", ptr->name);
  177.          ptr=ptr->next;
  178.      }
  179.      }
  180.      return;
  181. }
  182.  
  183. void load_record(FILE *fp, Nodeptr *headptr, Nodeptr *lastptr)
  184. {
  185.      Nodeptr load;
  186.      int node_size=sizeof(NodeItem);
  187.      int name_length,address_length;
  188.  
  189.      clrscr();
  190.      if((fp=fopen(FILENAME,"r"))==NULL)
  191.      {
  192.       printf("\a\n\nERROR!!!!FILE CAN'T BE OPEN!!!\n");
  193.      }
  194.      else
  195.      {
  196.       while(!feof(fp))
  197.       {
  198.           load=(Nodeptr)malloc(node_size);
  199.           fscanf(fp, "%d\n", &name_length);
  200.           fscanf(fp, "%d\n", &address_length);
  201.           fgets(load->name,name_length+1, fp);
  202.           fscanf(fp,"\n");
  203.           fgets(load->address, address_length+1, fp);
  204.           fscanf(fp, "%d\n",  &load->age);
  205.  
  206.           if((*headptr)==NULL)
  207.           {
  208.            (*headptr)=load;
  209.            (*lastptr)=load;
  210.           }
  211.           else
  212.           {
  213.            (*lastptr)->next=load;
  214.            (*lastptr)=load;
  215.           }
  216.       }
  217.       (*lastptr)->next=NULL;
  218.      }
  219.      fclose(fp);
  220.      return;
  221. }
  222.  
  223. void save_record(FILE *fp, Nodeptr *headptr)
  224. {
  225.      Nodeptr save;
  226.      int i;
  227.  
  228.      if ((fp=fopen(FILENAME,"w"))==NULL)
  229.      {
  230.       printf("\a\n\nERROR!!!!FILE CAN'T BE OPEN!!!\n");
  231.      }
  232.      else
  233.      {
  234.       save=(*headptr);  /****/
  235.  
  236.       while(save!=NULL)
  237.       {
  238.            fprintf( fp,"%d\n",  strlen(save->name));
  239.            fprintf( fp, "%d\n", strlen(save->address));
  240.            fprintf( fp, "%s\n", save->name);
  241.            fprintf( fp, "%s\n", save->address);
  242.            fprintf( fp, "%d\n", save->age);
  243.            save=save->next;
  244.       }
  245.      }
  246.      fclose(fp);
  247.      printf("\a\n\nSAVING DONE!!!!!\n");
  248.      printf("\nPress any key to continue.....");
  249.      getch();
  250.      return;
  251. }
  252. ---------------------------------22081592413785--
  253.